package com.hy.study.reduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 01 月 31 日 9:46: 27
 */
public class KeyValueReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable value = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        // 累加求和
        int sum = 0;
        for (IntWritable count : values) {
            sum += count.get();
        }
        value.set(sum);
        context.write(key, value);
    }
}
